package zhoufu.sz.sanxiao_check.ui.train;

import android.databinding.ViewDataBinding;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import cn.finalteam.galleryfinal.FunctionConfig;
import cn.finalteam.galleryfinal.GalleryFinal;
import cn.finalteam.galleryfinal.model.PhotoInfo;
import java.util.ArrayList;
import java.util.List;
import javax.inject.Inject;
import zhoufu.sz.sanxiao_check.R;
import zhoufu.sz.sanxiao_check.databinding.FragmentAddTrainBinding;
import zhoufu.sz.sanxiao_check.inject.components.DaggerLoginComponet;
import zhoufu.sz.sanxiao_check.inject.components.LoginComponet;
import zhoufu.sz.sanxiao_check.models.BaseModel;
import zhoufu.sz.sanxiao_check.presents.AddTrainPresenter;
import zhoufu.sz.sanxiao_check.ui.BaseActivity;
import zhoufu.sz.sanxiao_check.ui.adapter.GridAdapter;
import zhoufu.sz.sanxiao_check.ui.contract.BasicMsgItem2Contract;
import zhoufu.sz.sanxiao_check.utils.UIUtils;

/**
 * User:RCP
 * email:ruancaipu@gmail.com
 * Date:2016/8/4
 * 新增场所培训
 */
public class AddTrainActivity extends BaseActivity implements AdapterView.OnItemClickListener,
        BasicMsgItem2Contract.View
{
    FragmentAddTrainBinding _binding;

    private GridAdapter _adapter;


    @Inject
    AddTrainPresenter _presenter;

    @Override public void onCreate(@Nullable Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setTitle("新增场所培训");
    }

    @Override protected void initInject()
    {
        LoginComponet componet = DaggerLoginComponet.builder().appComponent(getAppComponent())
                .activityModule(getActivityModule()).build();
        componet.inject(this);
        _presenter.bindView(this);
    }

    @Override protected void initAfterOnCreate(ViewDataBinding binding)
    {
        _binding = (FragmentAddTrainBinding) binding;
        _binding.mGridView.setOnItemClickListener(this);
        _adapter = new GridAdapter(this, new ArrayList<String>(), R.layout.item_imageview, true);
        _binding.mGridView.setAdapter(_adapter);
    }

    @Override protected int setIndividualId()
    {
        return R.layout.fragment_add_train;
    }

    @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id)
    {
        if ( position == _adapter.getAllData().size() )
        {
            FunctionConfig config = new FunctionConfig.Builder()
                    .setEnableCamera(true)
                    .setMutiSelectMaxSize(3 - _adapter.getAllData().size()).build();
            GalleryFinal.openGalleryMuti(1, config, mResultCallback);
        }
    }

    @Override public boolean onCreateOptionsMenu(Menu menu)
    {
        getMenuInflater().inflate(R.menu.menu_save, menu);
        return super.onCreateOptionsMenu(menu);
    }

    @Override public boolean onOptionsItemSelected(MenuItem item)
    {
        if ( item.getItemId() == R.id.menu_save )
        {
            _presenter.save(_binding.mEtTitle.getText().toString(),
                    _binding.mEtContent.getText().toString(), _adapter.getAllData());
        }
        return super.onOptionsItemSelected(item);
    }

    private GalleryFinal.OnHanlderResultCallback mResultCallback
            = new GalleryFinal.OnHanlderResultCallback()
    {
        @Override public void onHanlderSuccess(int reqeustCode, List<PhotoInfo> resultList)
        {
            if ( resultList != null && resultList.size() > 0 )
            {
                for (PhotoInfo info : resultList)
                {
                    _adapter.add(info.getPhotoPath());
                }
            }
            _binding.mGridView.setAdapter(_adapter);
        }

        @Override public void onHanlderFailure(int requestCode, String errorMsg)
        {
            UIUtils.showSnackbar(_binding.getRoot(), errorMsg);
        }
    };

    @Override public String SID()
    {
        return getIntent().getStringExtra("SID");
    }

    @Override public void setModel(BaseModel baseModel)
    {

    }

    @Override public void updateSuccese()
    {
        finish();
    }

    @Override public void showToast(String msg)
    {
        UIUtils.showSnackbar(_binding.getRoot(), msg);
    }

    @Override public void pullFaild()
    {
        UIUtils.showSnackbar(_binding.getRoot(), R.string.toast_network_error);
    }

    @Override public void pullEnd()
    {
        hideProgreesDialog();
    }

    @Override public void pullStart()
    {
        showProgreesDialog();
    }
}
